package com.eveinfo.tools;

import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class Logging {

	private static Logging instance;
	
	public static Logging getInstance(){
		if(instance == null){
			instance = new Logging();
		}
		return instance;
	}
	
	private FileHandler file;
	private Logger logger;

	public Logging() {
		logger = Logger.getLogger("logging");
		(new File("/sdcard/eveinfo")).mkdir();
		new File("/sdcard/eveinfo/log.txt");
		try {
			file = new FileHandler("/sdcard/eveinfo/log.txt", 5000000, 3, true);
			file.setFormatter(new SimpleFormatter());
			logger.addHandler(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Logger getLogger() {
		return logger;
	}

	public void close() {
		if(file != null){
			file.close();
		}
	}

	public void e(String msg) {
		logger.log(Level.SEVERE, msg);
	}

	public void e(Exception e) {
		logger.log(Level.SEVERE, e.toString());
		final StackTraceElement[] stackTrace = e.getStackTrace();
		int i = 0;
		for (StackTraceElement element : stackTrace) {
			if (i > 15)
				return;
			logger.log(Level.SEVERE, element.toString());
			i++;
		}
	}
}
